package com.itinsys.enma.web;


import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itinsys.enma.domain.School;
import com.itinsys.enma.domain.response.SchoolResponse;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.manager.SchoolManager;

/**
 * @author rperez
 *
 */

@Controller
@RequestMapping({ "/school" })
public class SchoolFormController extends FormController {
	protected final Log logger = LogFactory.getLog("controller");
	
	@Autowired
	private SchoolManager schoolManager;

	@Autowired
	private ResponseManager responseManager;
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	@ResponseBody
	public SchoolResponse getAllSchool() {
		return schoolManager.getAllSchools();
	}

	@RequestMapping(value = { "/add" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public SchoolResponse addSchool(@RequestBody Map<String,String> schoolDTO,HttpServletResponse response) {
		final String id = (String) schoolDTO.get("id");
		final String description = (String) schoolDTO.get("description");
		School newSchool = new School();
		newSchool.setDescription(description);
		newSchool.setId(id);
		return this.schoolManager.addSchool(newSchool);
	}

	@RequestMapping(value = { "/edit" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public SchoolResponse editSchool(@RequestBody Map<String,String> schoolDTO,HttpServletResponse response) {
		School school = new School();
		school.setId(schoolDTO.get("id"));
		school.setDescription(schoolDTO.get("description"));
		return this.schoolManager.updateSchool(school);
	}
	

	@RequestMapping(value = { "/delete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public SchoolResponse deleteSchool(@RequestBody Map<String,String> schoolDTO,HttpServletResponse response) {
		School school = new School();
		school.setId(schoolDTO.get("id"));
		return this.schoolManager.deleteSchool(school);
	}
	


	
}
